package com.hnkywl.sts.service.fxssh;

import com.hnkywl.sts.dao.organ.FkfsDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.zjgl.BzjDao;
import com.hnkywl.sts.entity.base.Sjzdz;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 客户相关服务端接口Service
 * Created by Fei on 2015/12/8.
 */
@Service
public class KhfwService {
    private FkfsDao fkfsDao;
    private BzjDao bzjDao;
    private GsjgDao gsjgDao;

    public String countDlf(String customName, Integer fklx, Integer sdays, Integer edays, Double weight) {
        if (fklx.equals(0)) {
            fklx = 8;
        } else {
            fklx = 9;
        }
        Double sValue = this.fkfsDao.findFkgsValue(customName, sdays, fklx, weight);
        Double eValue = this.fkfsDao.findFkgsValue(customName, edays, fklx, weight);
        return "{\"code\" :\"0\"," + "\"message\":\"操作成功\"," + "\"sagentFee\":\"" + sValue + "（元）\"," + "\"eagentFee\":\"" + eValue + "（元）\"}";
    }

    /**
     * 查询保证金，保证金比例
     *
     * @param khqc
     * @return
     */
    public String queryMarginByKhqc(String khqc) {
        StringBuilder sb = new StringBuilder();
        String bzjje = this.bzjDao.queryMarginByKhqc(khqc);
        String bzjbl = this.bzjDao.queryBzjblByKhqc(khqc);
        sb.append("{\"code\":\"0\",\"message\":\"操作成功\",\"cashDeposit\":\"" + bzjje + "（元）\",\"bzjbl\":\"" + bzjbl + "%\"}");
        return sb.toString();
    }

    /**
     * 根据全称查询代理费规则
     *
     * @param qc
     * @return
     */
    public String queryAgentFeeRuleByQc(String qc) {
        String result;
        //代理费规则
        String agentfee = "";
        Gsjg gsjg = gsjgDao.findGsjgIdByQc(qc);
        if (gsjg != null) {
            //根据gsjg_id获取list_fkfs
            List<Map> listMap = fkfsDao.findAgentFeeRuleByGsjgId(gsjg.getId());
            if (null != listMap || listMap.size() > 0) {
                agentfee += "[";
                for (Map map : listMap) {
                    String pay_type = map.get("fklx").toString();
                    String range = map.get("range").toString();
                    String rate = map.get("rate").toString();
                    agentfee += "{\"pay_type\":\"" + pay_type + "\",\"range\":\"" + range + "\",\"rate\":\"" + rate + "\"},";
                }
                agentfee = agentfee.substring(0, agentfee.length() - 1) + "]";
                result = "{\"code\":\"0\",\"message\":\"操作成功\",\"agentFeeRules\":" + agentfee + "}";
            } else {
                result = "{\"code\":\"1\",\"message\":\"没有查询到代理费标准\",\"agentFeeRules\":[]}";
            }
        } else {
            result = "{\"code\":\"1\",\"message\":\"没有此公司\",\"agentFeeRules\":[]}";
        }
        return result;
    }

    @Autowired
    public void setBzjDao(BzjDao bzjDao) {
        this.bzjDao = bzjDao;
    }

    @Autowired
    public void setFkfsDao(FkfsDao fkfsDao) {
        this.fkfsDao = fkfsDao;
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }

}
